import pygame
from pygame.sprite import Sprite
pygame.mixer.init()
# bullet_sound = pygame.mixer.Sound("musics/2.wav")
#
class Bullet(Sprite):

    #一个对飞船发射子弹管理的类
    def __init__(self,ai_settings,screen,ship):
        #在飞船所在处的位置创建一个子弹对象
        if ai_settings.op == 0:
            bullet_sound = pygame.mixer.Sound("musics/2.wav")
            bullet_sound.set_volume(0.5)
        elif ai_settings.op == 1:
            bullet_sound = pygame.mixer.Sound("musics/七彩雷电弹.wav")
        elif ai_settings.op == 2 or ai_settings.op == 3:
            bullet_sound = pygame.mixer.Sound("musics/寒冰弹.mp3")
        else:
            bullet_sound = pygame.mixer.Sound("musics/龙年限定.mp3")
        super(Bullet, self).__init__()
        self.tt = ai_settings
        self.screen = screen
        bullet_sound.play()
        #在(0,0)处创建一个表示子弹的矩形，再设置正确的位置
        self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        #存储用小数表示的子弹位置
        self.y = float(self.rect.y)
        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor
    def update(self):
        #向上移动子弹
        #更新表示子弹位置的小数值
        self.y -= self.speed_factor
        #更新表示子弹的rect的位置
        self.rect.y = self.y
    def draw_bullet(self):
        if self.tt.op == 0:
          pygame.draw.rect(self.screen, self.color, self.rect)
        elif self.tt.op == 1:
            bullet_image = pygame.image.load("images/zd1.png")
            # 缩放子弹图像
            bullet_image = pygame.transform.scale(bullet_image, (89, 90))
            # self.rect.x =  89    启动外挂机制
            # self.rect.y = 90
            # 绘制子弹图像
            self.screen.blit(bullet_image, self.rect)
        elif self.tt.op == 2:
            bullet_image = pygame.image.load("images/zd2.png")
            # 缩放子弹图像
            bullet_image = pygame.transform.scale(bullet_image, (174, 126))
            # 绘制子弹图像
            self.screen.blit(bullet_image, self.rect)
        elif self.tt.op == 3:
            bullet_image = pygame.image.load("images/zd3.png")
            # 缩放子弹图像
            bullet_image = pygame.transform.scale(bullet_image, (300, 200))
            # 绘制子弹图像
            self.screen.blit(bullet_image, self.rect)
        elif self.tt.op == 4:
            bullet_image = pygame.image.load("images/龙年.png")
            # 缩放子弹图像
            bullet_image = pygame.transform.scale(bullet_image, (100, 300))
            # 绘制子弹图像
            self.screen.blit(bullet_image, self.rect)





